//
//  XMGSeeBigPictureViewController.m
//  BuDeJie19
//
//  Created by hjj on 2016/12/3.
//  Copyright © 2016年 hjjuny. All rights reserved.
//

#import "XMGSeeBigPictureViewController.h"
#import "XMGTopicItem.h"
#import <UIImageView+WebCache.h>
#import <SDImageCache.h>
#import <SVProgressHUD.h>
#import <Photos/Photos.h>
#import "XMGPhotoManager.h"
#define XMGAlbumTitle @"百思不得姐19"

@interface XMGSeeBigPictureViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) UIImageView *imageView;
@end

@implementation XMGSeeBigPictureViewController

/*
 如何学习新的框架 (百度)
 
 1.了解这个框架有哪些常用类
 2.查看苹果官方文档 (1.想看框架有哪些类，Reference 2.想了解框架怎么用，Guide)
 
 想了解一个类怎么去用，可以使用option
 
 */

/*
    PHPhotoLibrary：相簿（所有相册集合）
    PHAsset:图片
    PHAssetCollection:相册，所有相片集合
    PHAssetChangeRequest:创建，修改，删除图片
    PHAssetCollectionChangeRequest:创建，修改，删除相册
 
*/

- (IBAction)dismiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)save:(id)sender {
    // 保存图片到自己的自定义相册 Photos 导入<Photos/Photos.h>
    
    /*
        0.显示提示框，询问用户是否允许当前App访问相册
        1.每次都创建新的相册
     */
    
    // 获取用户授权状态
    
    /*
     PHAuthorizationStatusNotDetermined = 0, 不确定
     PHAuthorizationStatusRestricted, 家长控制,拒绝
     PHAuthorizationStatusDenied, 拒绝
     PHAuthorizationStatusAuthorized 授权
     */
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    
    if (status == PHAuthorizationStatusNotDetermined) { // 不确定
        
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            // 授权完成就会调用
            if (status == PHAuthorizationStatusAuthorized) {
                
                [XMGPhotoManager savePhoto:_imageView.image albumTitle:XMGAlbumTitle completionHandler:^(BOOL success, NSError *error) {
                    if (error) {
                        [SVProgressHUD showErrorWithStatus:@"保存失败"];
                    } else {
                        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
                    }

                }];
                
            }
        }];
        
    } else if (status == PHAuthorizationStatusAuthorized) {
        [XMGPhotoManager savePhoto:_imageView.image albumTitle:XMGAlbumTitle completionHandler:^(BOOL success, NSError *error) {
            if (error) {
                [SVProgressHUD showErrorWithStatus:@"保存失败"];
            } else {
                [SVProgressHUD showSuccessWithStatus:@"保存成功"];
            }
            
        }];
        
    } else {
        // 拒绝
        // 提示用户去打开授权
        [SVProgressHUD showInfoWithStatus:@"进入设置界面->找到当前应用->打开允许访问相册开关"];
        
    }
    
}


#pragma mark - 保存图片
- (void)savePhoto
{
}

// 保存图片完成的时候调用
//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
//{
//
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIImageView *imageView = [[UIImageView alloc] init];
    _imageView = imageView;
    if (_item.localImage) {
        imageView.image = _item.localImage;
    } else {
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.item.image2]];
    }
    
    [_scrollView addSubview:imageView];
    
    CGFloat h = XMGScreenW / _item.width * _item.height;
    
    imageView.frame = CGRectMake(0, 0, XMGScreenW, h);
    
    if (_item.is_bigPicture) {
        // 大图
        _scrollView.contentSize = CGSizeMake(0, h);
        
        // 进行缩放
        _scrollView.delegate = self;
        
        if (_item.height > h) {
            // 设置最大缩放比例
            _scrollView.maximumZoomScale = _item.height / h;
            
        }
        
    } else {
        
        // 小图
        imageView.center = CGPointMake(XMGScreenW * 0.5, XMGScreenH * 0.5);
    }
    
}

#pragma mark - UIScrollViewDelegate
// 功能：返回需要缩放控件
// 什么时候调用：进行缩放的时候才会调用
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
